package com.wetalk.gateway.utils;

import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Component
public class JwtUtils {

    public static String jwt;

    //HS256签名算法
    public static String jwtCreate(Map<String,Object> cliams){
        String mymessage = Jwts.builder()
                .signWith(SignatureAlgorithm.HS256, "myMessage")//签名算法&密钥
                .setClaims(cliams)//传输内容
                .setExpiration(new Date(System.currentTimeMillis() + 10*60*1000))//有效期，时间毫秒
                .compact();
        return mymessage;
    }

    public static Claims jwtRead(String secretNum){
        Claims claim =Jwts.parser()
                .setSigningKey("myMessage")//密钥
                .parseClaimsJws(secretNum)
                .getBody();
        jwt=secretNum;
        return claim;
    }

    public static void main(String[] args) {
//        Map<String, Object> cliams = new HashMap<>();
//        cliams.put("name","12");
//        cliams.put("password","12");
//        String mymessage = Jwts.builder()
//                .signWith(SignatureAlgorithm.HS256, "myMessage")//签名算法&密钥
//                .setClaims(cliams)//传输内容
//                .setExpiration(new Date(System.currentTimeMillis() + 1000 * 3600))//有效期，时间毫秒
//                .compact();
//        System.out.println(mymessage);


        String secretNum = "eyJhbGciOiJIUzI1NiJ9.eyJwYXNzd29yZCI6IjEyIiwibmFtZSI6IjEyIiwiZXhwIjoxNzIzMDQ4ODAxfQ.-_f0Yk7jozFxsMxMg7m65FtJgfX1CSMKNpGeverxFls";
        Claims claim =Jwts.parser()
                .setSigningKey("myMessage")//密钥
                .parseClaimsJws(secretNum)
                .getBody();
        System.out.println(claim);
        System.out.println(claim.get("name"));
        System.out.println(claim.get("password"));
        System.out.println(claim.getExpiration());


    }


}
